package main;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import javax.swing.JTextField;

import com.aptech.mwg.etown.pojo.Account;
import com.aptech.mwg.etown.pojo.exception.UpdateDBException;
import com.aptech.mwg.etown.server.dao.impl.AccountDAOImpl;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class upacc extends JDialog {

    private final JPanel contentPanel = new JPanel();
    private JTextField txtTxtusers;
    private JTextField txtTxtemail;
    private JTextField txtTxtphone;
    private JTextField txtTxtdc;
    private JTextField txtTxtpos;
    private int accID;
    
    public void setAccID(int accID) {
        this.accID = accID;
    }
    
    public int getAccID() {
        return accID;
    }

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
	try {
	    upacc dialog = new upacc();
	    dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
	    dialog.setVisible(true);
	} catch (Exception e) {
	    e.printStackTrace();
	}
    }

    /**
     * Create the dialog.
     */
    public upacc() {
    	setTitle("Update Account");
	setBounds(100, 100, 388, 225);
	getContentPane().setLayout(new BorderLayout());
	contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
	getContentPane().add(contentPanel, BorderLayout.CENTER);
	GridBagLayout gbl_contentPanel = new GridBagLayout();
	gbl_contentPanel.columnWidths = new int[]{0, 0, 0};
	gbl_contentPanel.rowHeights = new int[]{0, 0, 0, 0, 0, 0};
	gbl_contentPanel.columnWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
	gbl_contentPanel.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
	contentPanel.setLayout(gbl_contentPanel);
	{
		JLabel lblAccountName = new JLabel("Account Name: ");
		GridBagConstraints gbc_lblAccountName = new GridBagConstraints();
		gbc_lblAccountName.anchor = GridBagConstraints.WEST;
		gbc_lblAccountName.insets = new Insets(0, 0, 5, 5);
		gbc_lblAccountName.gridx = 0;
		gbc_lblAccountName.gridy = 0;
		contentPanel.add(lblAccountName, gbc_lblAccountName);
	}
	{
		txtTxtusers = new JTextField();
		GridBagConstraints gbc_txtTxtusers = new GridBagConstraints();
		gbc_txtTxtusers.insets = new Insets(0, 0, 5, 0);
		gbc_txtTxtusers.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtTxtusers.gridx = 1;
		gbc_txtTxtusers.gridy = 0;
		contentPanel.add(txtTxtusers, gbc_txtTxtusers);
		txtTxtusers.setColumns(10);
	}
	{
		JLabel lblEmail = new JLabel("Email: ");
		GridBagConstraints gbc_lblEmail = new GridBagConstraints();
		gbc_lblEmail.insets = new Insets(0, 0, 5, 5);
		gbc_lblEmail.anchor = GridBagConstraints.WEST;
		gbc_lblEmail.gridx = 0;
		gbc_lblEmail.gridy = 1;
		contentPanel.add(lblEmail, gbc_lblEmail);
	}
	{
		txtTxtemail = new JTextField();
		GridBagConstraints gbc_txtTxtemail = new GridBagConstraints();
		gbc_txtTxtemail.insets = new Insets(0, 0, 5, 0);
		gbc_txtTxtemail.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtTxtemail.gridx = 1;
		gbc_txtTxtemail.gridy = 1;
		contentPanel.add(txtTxtemail, gbc_txtTxtemail);
		txtTxtemail.setColumns(10);
	}
	{
		JLabel lblPhone = new JLabel("Phone: ");
		GridBagConstraints gbc_lblPhone = new GridBagConstraints();
		gbc_lblPhone.anchor = GridBagConstraints.WEST;
		gbc_lblPhone.insets = new Insets(0, 0, 5, 5);
		gbc_lblPhone.gridx = 0;
		gbc_lblPhone.gridy = 2;
		contentPanel.add(lblPhone, gbc_lblPhone);
	}
	{
		txtTxtphone = new JTextField();
		GridBagConstraints gbc_txtTxtphone = new GridBagConstraints();
		gbc_txtTxtphone.insets = new Insets(0, 0, 5, 0);
		gbc_txtTxtphone.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtTxtphone.gridx = 1;
		gbc_txtTxtphone.gridy = 2;
		contentPanel.add(txtTxtphone, gbc_txtTxtphone);
		txtTxtphone.setColumns(10);
	}
	{
		JLabel lblAddress = new JLabel("Address: ");
		GridBagConstraints gbc_lblAddress = new GridBagConstraints();
		gbc_lblAddress.anchor = GridBagConstraints.WEST;
		gbc_lblAddress.insets = new Insets(0, 0, 5, 5);
		gbc_lblAddress.gridx = 0;
		gbc_lblAddress.gridy = 3;
		contentPanel.add(lblAddress, gbc_lblAddress);
	}
	{
		txtTxtdc = new JTextField();
		GridBagConstraints gbc_txtTxtdc = new GridBagConstraints();
		gbc_txtTxtdc.insets = new Insets(0, 0, 5, 0);
		gbc_txtTxtdc.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtTxtdc.gridx = 1;
		gbc_txtTxtdc.gridy = 3;
		contentPanel.add(txtTxtdc, gbc_txtTxtdc);
		txtTxtdc.setColumns(10);
	}
	{
		JLabel lblPosition = new JLabel("Position: ");
		GridBagConstraints gbc_lblPosition = new GridBagConstraints();
		gbc_lblPosition.anchor = GridBagConstraints.WEST;
		gbc_lblPosition.insets = new Insets(0, 0, 0, 5);
		gbc_lblPosition.gridx = 0;
		gbc_lblPosition.gridy = 4;
		contentPanel.add(lblPosition, gbc_lblPosition);
	}
	{
		txtTxtpos = new JTextField();
		GridBagConstraints gbc_txtTxtpos = new GridBagConstraints();
		gbc_txtTxtpos.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtTxtpos.gridx = 1;
		gbc_txtTxtpos.gridy = 4;
		contentPanel.add(txtTxtpos, gbc_txtTxtpos);
		txtTxtpos.setColumns(10);
	}
	{
	    JPanel buttonPane = new JPanel();
	    buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
	    getContentPane().add(buttonPane, BorderLayout.SOUTH);
	    {
		JButton okButton = new JButton("OK");
		okButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			    boolean result = false;
				Account account =  new Account();
				
				account.setAccountId(getAccID());
				account.setLoginName(txtTxtusers.getText());
				account.setEmail(txtTxtemail.getText());
				account.setAddress(txtTxtdc.getText());
				account.setPhone(txtTxtphone.getText());
				account.setPosition(txtTxtpos.getText());
				
				AccountDAOImpl accountImpl = new AccountDAOImpl();
				try {
					result = accountImpl.update(account);
					if(result = true){
					    setVisible(false);
					    accManager acc = new accManager();
					    acc.loadtable();
					}
				} catch (UpdateDBException e1) {
					e1.printStackTrace();
					
				}
				if(result == true){
				    System.out.println("update success!");
				}else{
				    System.out.println("update fail");
				}
				
			}
		});
		okButton.setActionCommand("OK");
		buttonPane.add(okButton);
		getRootPane().setDefaultButton(okButton);
	    }
	    {
	    	JButton btnReset = new JButton("Reset");
	    	buttonPane.add(btnReset);
	    }
	    {
		JButton cancelButton = new JButton("Cancel");
		cancelButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			    setVisible(false);
			}
		});
		cancelButton.setActionCommand("Cancel");
		buttonPane.add(cancelButton);
	    }
	}
    }

}
